﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class Admin_Mail_Add : System.Web.UI.Page
{
    SqlConnection con;
    int IDMail;
    int newID;
    float totalfee;
    float chargeWeight;
    float chargeDeliver;
    float distance;
    //Connect to database
    void connect()
    {
       
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["OPOConnectionString"].ToString());
        con.Open();
        //if (con != null)
        //{
        //    Response.Write("Connect Successful.");
        //}
       
    }
    //Get ID Mail
    void getID() 
    {
        string select = "select MAX(MailID) from Mail";
        SqlCommand cmd1 = new SqlCommand(select, con);
        SqlDataReader dr = cmd1.ExecuteReader();
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                IDMail =  Convert.ToInt16(dr[0].ToString());
                newID=IDMail+1;
                lbIDMail.Text =Convert.ToString(newID) ; 
            }
        }
        

        dr.Close();
    }
    //Reset all textbox
    void reset() 
    {
  
        txtRAddress.Text = "";
        txtRName.Text = "";
        txtRPhone.Text = "";
        txtSAddress.Text = "";
        txtSEmail.Text = "";
        txtSName.Text = "";
        txtSPhone.Text = "";
        txtZipCode.Text = "";
    }
    // Add new mail
    void add()
    {
        try
        {
            //string select = "select * Mail";
            //SqlCommand cmd1 = new SqlCommand(select, con);
            //SqlDataReader dr = cmd1.ExecuteReader();
            //if (dr.HasRows)
            //{
            //    while (dr.Read())
            //    {
            //        if (dr[1].ToString() == txtMailID.Text)
            //        {
            //            Response.Write("Mail ID already exists");
            //            return;
            //        }
            //    }
            //}
            //dr.Close();
            string strInsert = "Insert Mail (BranchID,DistanceID,MTypeID,DTypeID,WeightID,ZipCode,DateReceipt,SenderName,SenderAddress,SenderEmail,SenderPhone, ReceiverName,ReceiverAddess,ReceiverPhone,TotalFee)Values  (@BranchID,@DistanceID,@MTypeID,@DTypeID,@WeightID,@ZipCode,@DateReceipt,@SenderName,@SenderAddress,@SenderEmail,@SenderPhone,@ReceiverName,@ReceiverAddess,@ReceiverPhone,@TotalFee)";
            SqlCommand cmd = new SqlCommand(strInsert, con);
          
            cmd.Parameters.AddWithValue("@BranchID",ddlBranch.SelectedValue);
            cmd.Parameters.AddWithValue("@DistanceID",ddlDOMail.SelectedValue);
            cmd.Parameters.AddWithValue("@MTypeID",ddlTOMail.SelectedValue);
            cmd.Parameters.AddWithValue("@DTypeID", ddlTODeliver.SelectedValue);
            cmd.Parameters.AddWithValue("@WeightID", ddlWOMail.SelectedValue);
            cmd.Parameters.AddWithValue("@ZipCode", txtZipCode.Text);
            cmd.Parameters.AddWithValue("@DateReceipt",lbDateReceipt.Text);
            cmd.Parameters.AddWithValue("@SenderName",txtSName.Text);
            cmd.Parameters.AddWithValue("@SenderAddress", txtSAddress.Text);
            cmd.Parameters.AddWithValue("@SenderEmail", txtSEmail.Text);
            cmd.Parameters.AddWithValue("@SenderPhone",txtSPhone.Text);
            cmd.Parameters.AddWithValue("@ReceiverName",txtRName.Text);
            cmd.Parameters.AddWithValue("@ReceiverAddess", txtRAddress.Text);
            cmd.Parameters.AddWithValue("@ReceiverPhone", txtRPhone.Text);
            cmd.Parameters.AddWithValue("@TotalFee",lbTotalFee.Text);                 
            cmd.ExecuteNonQuery();
            MessageDialog.MessDialog("Create new Mail successful.",this);
            getID();
        }
        catch (Exception e)
        {
            Response.Write(e.Message);
        }
    }
    //Total Fee
    void total() 
    {
        int WID;//ID Weight of Mail
        int TID;// ID Type of Deliver
        int DID;//ID Distanceof mail 
      
        WID = Convert.ToInt16(ddlWOMail.SelectedValue);
        TID = Convert.ToInt16(ddlTODeliver.SelectedValue);
        DID = Convert.ToInt16(ddlDOMail.SelectedValue);
        SqlCommand cmd = new SqlCommand("Select WeightCharge from WeightOfMail where WeightID= '" + WID + "'", con);
        SqlDataReader dread = cmd.ExecuteReader();
        if (dread.Read())
        {
            chargeWeight = float.Parse(dread.GetValue(0).ToString());
            
        }
        dread.Close();
        SqlCommand cmd1 = new SqlCommand("Select DTypeCharge from TypeofDeliver where DTypeID= '" + TID + "'", con);
        SqlDataReader dread1 = cmd1.ExecuteReader();
        if (dread1.Read())
        {
            chargeDeliver=  float.Parse(dread1.GetValue(0).ToString());
            
        }
        dread1.Close();
        SqlCommand cmd2 = new SqlCommand("Select DistanceCharge from DistanceofMail where DistanceID= '" + DID + "'", con);
        SqlDataReader dread2 = cmd2.ExecuteReader();
        if (dread2.Read())
        {
            distance=  float.Parse(dread2.GetValue(0).ToString());
        
        }
        dread2.Close(); 
        lbTotalFee.Text=Convert.ToString((chargeWeight+chargeDeliver)*distance);
   
        // string total==Convert.ToString((float.Parse(ddlTODeliver.SelectedValue) + float.Parse(charge)) * float.Parse(ddlDOMail.SelectedValue));
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        connect();
        getID();
    }
 
    protected void lbDateReceipt_Load(object sender, EventArgs e)
    {
        lbDateReceipt.Text = DateTime.Now.ToShortDateString();
    }
    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        reset();
    }
    protected void imgbAdd_Click(object sender, ImageClickEventArgs e)
    {
        if (lbTotalFee.Text == "0")
        {
            MessageDialog.MessDialog("You must calculate fee", this);
        }
        else 
        {
            add();
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        total();
    }
}